The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(tsibble)
Registered S3 method overwritten by 'tsibble':
method from
as_tibble.grouped_df dplyr
Attaching package: 'tsibble'
The following objects are masked from 'package:base':
intersect, setdiff, union
# Example: Cache la Poudre River at Mouth (USGS site 06752260)poudre_flow <-readNWISdv(siteNumber ="06752260", # Download data from USGS for site 06752260parameterCd ="00060", # Parameter code 00060 = discharge in cfs)startDate ="2013-01-01", # Set the start dateendDate ="2023-12-31") |># Set the end daterenameNWISColumns() |># Rename columns to standard names (e.g., "Flow", "Date")mutate(Date =yearmonth(Date)) |># Convert daily Date values into a year-month format (e.g., "2023 Jan")group_by(Date) |># Group the data by the new monthly Datesummarise(Flow =mean(Flow)) # Calculate the average daily flow for each month
Use as_tsibble() to convert the data.frame into a tsibble object. This will allow you to use the feast functions for time series analysis
poudre_tbl <-as_tsibble(poudre_flow)
Using `Date` as index variable.
head(poudre_tbl)
# A tsibble: 6 x 2 [1M]
Date Flow
<mth> <dbl>
1 2013 Jan 18.1
2 2013 Feb 18.0
3 2013 Mar 8.21
4 2013 Apr 5.94
5 2013 May 333.
6 2013 Jun 300.
Plotting the time series
Use ggplot to plot the time series data. Animate this plot with plotly
library(ggplot2)library(plotly)
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
poudre_plot <-ggplot(poudre_tbl, aes(x = Date, y = Flow)) +geom_line(color ="steelblue") +labs(title ="Monthly Streamflow of Cache la Poudre River",x ="Date", y ="Average Monthly Flow") +theme_minimal()ggplotly(poudre_plot)
Subseries
Use gg_subseries to visualize the seasonal patterns in the data. This will help you identify any trends or seasonal cycles in the streamflow data.
library(feasts)
Loading required package: fabletools
gg_subseries(poudre_tbl) +labs(title ="Monthly Streamflow",x ="Year", y ="Flow") +theme_minimal()
Plot variable not specified, automatically selected `y = Flow`
Describe what you see in the plot. How are “seasons” defined in this plot? What do you think the “subseries” represent?
The plot shows a seasonal pattern in streamflow for the Cache la Poudre River. You can see that streamflow peaks in May-June, which can be classified as late spring and early summer. This is most likely due to snow melt as the weather gets warmer in these months. You can also see that streamflow is lowest in November-Febuary, which can be classified as late fall and winter.
Decompose
Use the model(STL(…)) pattern to decompose the time series data into its components: trend, seasonality, and residuals. Chose a window that you feel is most appropriate to this data…
Describe what you see in the plot. How do the components change over time? What do you think the trend and seasonal components represent?
The trend component increases from 2013-2015ish, then from 2016-2020 decreases, and after 2020 remains fairly stable. This trend component shows the fluctuation in streamflow over time, which could be reflective of changes in watershed hydrology. The seasonality component shows repeated peaks and valleys throughout the years. The peaks for this component are most likely during spring since there is likely be more snowmelt during that time (creating more streamflow). The remainder component varies a lot over the time period in the plot.